package com.red.star.wechat.data.utils;

import org.springframework.util.Assert;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * AES加密解密工具类
 *
 * @author AMGuo
 */
public class AESUtil {
    /**
     * 加密用的Key 可以用26个字母和数字组成
     * 此处使用AES-128-CBC加密模式，key需要为16位。
     **/
    /**
     * 密钥
     */
    private static String sKey = "1234567890123456";
    /**
     * iv：密钥偏移量
     */
    private static String ivParameter = "1234567890123456";

    private static final String KEY_MODE = "AES";

    private static final String KEY_FORMAT = "AES/CBC/PKCS5Padding";

    private static final String ENCODING_FORMAT = "UTF-8";

    /**
     * 加密
     *
     * @param sSrc
     * @return
     * @throws Exception
     */
    public static String encrypt(String sSrc) {
        return encrypt(sSrc, sKey, ivParameter);
    }

    /**
     * 加密
     *
     * @param sSrc
     * @return
     * @throws Exception
     */
    public static String encrypt(String sSrc, String key, String iv) {
        Assert.notNull(sSrc, "sSrc is null");
        Assert.notNull(key, "key is null");
        Assert.notNull(iv, "iv is null");
        try {
            Cipher cipher = Cipher.getInstance(KEY_FORMAT);
            byte[] raw = key.getBytes();
            SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_MODE);
            //使用CBC模式，需要一个向量iv，可增加加密算法的强度
            IvParameterSpec _iv = new IvParameterSpec(iv.getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, _iv);
            byte[] encrypted = cipher.doFinal(sSrc.getBytes(ENCODING_FORMAT));
            //此处使用BASE64做转码。
            return new BASE64Encoder().encode(encrypted);
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    /**
     * 解密
     *
     * @param sSrc
     * @return
     * @throws Exception
     */
    public static String decrypt(String sSrc) {
        return decrypt(sSrc, sKey, ivParameter);
    }

    /**
     * 解密
     *
     * @param sSrc
     * @return
     * @throws Exception
     */
    public static String decrypt(String sSrc, String key, String iv) {
        Assert.notNull(sSrc, "sSrc is null");
        Assert.notNull(key, "key is null");
        Assert.notNull(iv, "iv is null");
        try {
            byte[] raw = key.getBytes();
            SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_MODE);
            Cipher cipher = Cipher.getInstance(KEY_FORMAT);
            IvParameterSpec _iv = new IvParameterSpec(iv.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, _iv);
            //先用base64解密
            byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);
            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original, ENCODING_FORMAT);
            return originalString;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }


    public static void main(String[] args) throws Exception {
        // 需要加密的字串
        String cSrc = "6216606300001692845";
        System.out.println("加密前的字串是：" + cSrc);
        // 加密
        String enString = encrypt(cSrc);
        System.out.println("加密后的字串是：" + enString);
        // 解密
        String DeString = decrypt(enString);
        System.out.println("解密后的字串是：" + DeString);
    }
}